namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class SubmissionHandlerSettingFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SubmissionHandlerSetting>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("handler")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            SubmissionHandlerSetting actual = (
                "<handler name='test'>" +
                    "<processor id='1' />" +
                    "<processor id='2'></processor>" +
                    "<processor id='3' />" +
                "</handler>").Deserialize<SubmissionHandlerSetting>();
            
            Assert.Equal<string>("test", actual.Name);

            Assert.Equal<int>(3, actual.Processors.Count);
        }

        [Fact]
        public void deserializeEmpty()
        {
            SubmissionHandlerSetting actual = "<handler />".Deserialize<SubmissionHandlerSetting>();

            Assert.Null(actual.Name);
        }

        [Fact]
        public void serialize()
        {
            SubmissionHandlerSetting obj = new SubmissionHandlerSetting
            {
                Name = "test",
                Processors =
                {
                    new SubmissionProcessorSetting
                    {
                        Id = "123"
                    }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/handler[@name='test']/processor[@id='123'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SubmissionHandlerSetting());
        }

        [Fact]
        public void prop_Name()
        {
            Assert.True(new PropertyExpectations(typeof(SubmissionHandlerSetting).GetProperty("Name"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Processors()
        {
            Assert.True(new PropertyExpectations(typeof(SubmissionHandlerSetting).GetProperty("Processors"))
                .TypeIs<IList<SubmissionProcessorSetting>>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void op_Process_FormSubmission()
        {
            ServiceLocator.Clear();

            try
            {
                ServiceLocator.Register<IProcessSubmission, TestIProcessSubmission>("component");

                SubmissionHandlerSetting obj = new SubmissionHandlerSetting
                {
                    Processors =
                    {
                        new SubmissionProcessorSetting
                        {
                            Id = "component"
                        }
                    }
                };

                Assert.IsAssignableFrom<SeeOtherResult>(obj.Process(new FormSubmission()));
            }
            finally
            {
                ServiceLocator.Reset();
            }
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new SubmissionHandlerSetting() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new SubmissionHandlerSetting() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new SubmissionHandlerSetting() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}